package bit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class leetcode90子集2 {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        Arrays.sort(nums);
        dfs(result, list, nums, false, 0, nums.length);
        return result;
    }

    public void dfs(List<List<Integer>> result, List<Integer> list, int[] nums, boolean choosePre, int level, int n) {
        if (level == n) {
            result.add(new ArrayList<>(list));
        } else {
            dfs(result, list, nums, false, level + 1, n);
            if (!choosePre && level > 0 && nums[level] == nums[level - 1]) {
                return;
            }
            list.add(nums[level]);
            dfs(result, list, nums, true, level + 1, n);
            list.remove(list.size() - 1);
        }
    }
}
